/*==============================================================================
Do-file for: Reconsidering Three Puzzles of Chinese Popular Nationalism: 
             Insights from Nationwide Longitudinal CSGS Data
==============================================================================*/

//------------------------------------------------------------------------------
// 0. PREPARATION
//------------------------------------------------------------------------------

// Set Stata version for reproducibility

version 18.5

//------------------------------------------------------------------------------
// 1. DATA PREPARATION
//------------------------------------------------------------------------------

use reg_data.dta, clear

tabulate education, generate(education_dummy)
rename education_dummy1 elementary
rename education_dummy2 junior
rename education_dummy3 high
rename education_dummy4 university
rename education_dummy5 graduate

tabulate demo_values, generate(demo_values_dummy)
rename demo_values_dummy1 substantive_demo
rename demo_values_dummy2 procedural_demo
rename demo_values_dummy3 ambiguous_demo

global controls "gender age pol_affiliation income residency ethnicity students netizens econ_performance auth_values"

//------------------------------------------------------------------------------
// 2. ANALYSIS FOR TABLE A1: DESCRIPTIVE STATISTICS & CORRELATIONS
//------------------------------------------------------------------------------

*--- Descriptive Statistics ---*

svyset [pweight=weight]

// 2015 Sample
svy: mean nationalism elementary junior high university graduate substantive_demo ///
    procedural_demo ambiguous_demo int_threat $controls if year==2015
estat sd

// 2019 Sample
svy: mean nationalism elementary junior high university graduate substantive_demo ///
    procedural_demo ambiguous_demo int_threat $controls if year==2019
estat sd

// Pooled Sample (2015 & 2019)
svy: mean nationalism elementary junior high university graduate substantive_demo ///
    procedural_demo ambiguous_demo int_threat $controls
estat sd


*--- Bivariate Correlation Analysis ---*

// 2015 Sample
wmtcorr nationalism elementary junior high university graduate /// 
	substantive_demo procedural_demo ambiguous_demo int_threat ///
	$controls [pweight=weight] if year==2015, b(4) p(%9.5f)

// 2019 Sample
wmtcorr nationalism elementary junior high university graduate /// 
	substantive_demo procedural_demo ambiguous_demo int_threat ///
	$controls [pweight=weight] if year==2019, b(4) p(%9.5f)

// Pooled Sample (2015 & 2019)
wmtcorr nationalism elementary junior high university graduate /// 
	substantive_demo procedural_demo ambiguous_demo int_threat ///
	$controls [pweight=weight], b(4) p(%9.5f)


//------------------------------------------------------------------------------
// 3. ANALYSIS FOR TABLE A2: MULTIVARIATE REGRESSION MODELS
//------------------------------------------------------------------------------

*--- Model 1 (2015) ---*
// Regression for the 2015 sample with province fixed effects
reg nationalism elementary junior university graduate substantive_demo ambiguous_demo int_threat ///
	$controls i.province_code [pweight=weight] ///
    if year == 2015, robust
est store m1

*--- Model 2 (2019) ---*
// Regression for the 2019 sample with province fixed effects
reg nationalism elementary junior university graduate substantive_demo ambiguous_demo int_threat ///
    $controls i.province_code [pweight=weight] ///
    if year == 2019, robust
est store m2

*--- Model 3 (Pooled) ---*
// Pooled regression with year fixed effects, province fixed effects,
// and an interaction term between year and education level.
// ib3.education sets "High school" as the base category for education.
reg nationalism substantive_demo ambiguous_demo int_threat i.year##ib3.education ///
	$controls i.province_code [pweight=weight], robust
est store m3


//------------------------------------------------------------------------------
// 4. CODE FOR FIGURE 1: REGRESSION COEFFICIENTS PLOT
//------------------------------------------------------------------------------

// Plot coefficients from Model 1 and Model 2 side-by-side
coefplot m1, bylabel("Model 1" " (2015)") ///
    || m2, bylabel("Model 2" " (2019)") ///
    keep(elementary junior university graduate substantive_demo ambiguous_demo int_threat) ///
    rename(substantive_demo="Substantive democratic values" ///
           ambiguous_demo="Ambiguous democratic values" ///
		   int_threat="Perceptions of international threats" ///
           elementary="Elementary school or less" ///
		   junior="Junior high school" ///
		   university="University / College" ///
		   graduate="Postgraduate") ///
    xline(0, lcolor(red) lwidth(medium)) ///
    byopts(row(1)) ///
    scheme(white_jet) ///
    graphregion(margin(medsmall)) ///
    xsize(6.5) ysize(4.5) ///
    mlabel(cond(@pval<.001, string(@b, "%9.3fc") + "***", ///
                cond(@pval<.01, string(@b, "%9.3fc") + "**", ///
                     cond(@pval<.05, string(@b, "%9.3fc") + "*", string(@b, "%9.3fc"))))) ///
    mlabsize(small) ///
    mlabposition(2)

//------------------------------------------------------------------------------
// 5. CODE FOR FIGURE 2: PREDICTIVE MARGINS PLOT
//------------------------------------------------------------------------------

// Calculate predictive margins of nationalism for each education level at year=2015 and year=2019
margins ib3.education, at(year=(2015 2019))

// Plot the predictive margins
marginsplot, ///
	x(education) ///
	plot1opts(msymbol(O) mcolor(black) lcolor(black) lpattern(solid)) ///
	plot2opts(msymbol(T) mcolor(black) lcolor(black) lpattern(dash)) ///
	ytitle("Predicted Nationalism Score") ///
	xtitle("Educational Attainment") ///
	legend(label(1 "2015") label(2 "2019") order(1 2) pos(2) ring(0) col(1)) /// 
	xlabels(1 "Elementary school or less" 2 "Junior high school" 3 "High school" 4 "University / College" 5 "Postgraduate", ///
	angle(15) labsize(small)) ///
	scheme(white_jet) graphregion(color(white))

/*============================ END OF DO-FILE ============================*/
